package com.hyb.algorithm.data.struct.str;

/**
 * @author: ybhu
 * @create: 2021/09/29 02:38
 * 680. 验证回文字符串 Ⅱ
 * https://leetcode-cn.com/problems/valid-palindrome-ii/
 */
public class ValidPalindromeIi2 {

    public static void main(String[] args) {
        ValidPalindromeIi2 exe = new ValidPalindromeIi2();
        String s = "abc";
        boolean t = exe.validPalindrome(s);

        System.out.println(t);
    }


    public boolean validPalindrome(String s) {
        int len = s.length();

        int left=0;
        int right=len-1;

        char[] charArr=s.toCharArray();
        while(left<=right){
            if(charArr[left]!=charArr[right]){
                return validPalindromeSub(charArr,left+1,right)||validPalindromeSub(charArr,left,right-1);
            }

            left++;
            right--;
        }

        return true;
    }

    public boolean validPalindromeSub(char[] charArr,int left,int right){

        while(left<right){
            if(charArr[left]!=charArr[right]){
                return false;
            }

            left++;
            right--;
        }

        return true;
    }
}
